#pragma once

#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::cerr;
using std::endl;

class Handler
{
    public:
    int operator()(int sock_fd_,std::string client_ip_,int port_)
    {
       
        pthread_detach(pthread_self());
        char buffer[1024];
        while(1)
        {
            ssize_t size=read(sock_fd_,buffer,sizeof(buffer)-1);
            if(size >0)
            {
                buffer[size]='\0';
                cout<<"["<<client_ip_<<" : "<<port_<<"] say#"<<buffer<<endl;
                write(sock_fd_,buffer,size);
            }
            else if(size ==0 )
            {
                break;
            }
            else 
            {
                cout<<"["<<client_ip_<<" : "<<port_<<"] ";
                perror("Read  Socket Failed\n");
                close(sock_fd_);
            }
        }
        close(sock_fd_);
        cout<<"Client Close sock_fd_"<<endl;
        return 0;
    }
    
};

class Task
{
    public:
    Task(){}
    Task(int sock_fd,std::string client_ip,int port)
    :sock_fd_(sock_fd),client_ip_(client_ip),port_(port)
    {}

    ~Task()
    {}

    void Run()
    {
        handler_(sock_fd_,client_ip_,port_);
    }
    private:
    int sock_fd_;
    std::string client_ip_;
    int port_;
    Handler handler_;
};